﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public enum NodeType
{
    RootNode,
    Node,
    LeafNode
}
public class NodeTreeInfo : MonoBehaviour
{
    public NodeType _nodeType;

    public List<GameObject> childern = new List<GameObject>();

    RectTransform _rectTransform;

    Image backImg;
    Text titleText;
    Image pointer;
    //GameObject nodeLine;
    Toggle toggle;

    /// <summary>
    /// 关闭根节点前  该节点的状态  不包含根节点
    /// </summary>
    public bool beforeRootClose;
    void OnEnable()
    {
        toggle = transform.GetComponent<Toggle>();
    }
    private void Start()
    {

        toggle.onValueChanged.AddListener(NodeClick);

        _rectTransform = transform.GetComponent<RectTransform>();

        if (_nodeType != NodeType.RootNode)
        {
            gameObject.SetActive(false);
        }

        backImg = transform.Find("Background").GetComponent<Image>();
        titleText = transform.GetComponentInChildren<Text>();
        pointer = transform.Find("Pointer").GetComponent<Image>();
        //Init
        switch (_nodeType)
        {
            //次级节点的位置偏移
            case NodeType.Node:
                backImg.GetComponent<RectTransform>().anchoredPosition -= new Vector2(-40,0);
                titleText.GetComponent<RectTransform>().anchoredPosition -= new Vector2(-40, 0);
                break;
            case NodeType.LeafNode:
                backImg.GetComponent<RectTransform>().anchoredPosition -= new Vector2(-80, 0);
                titleText.GetComponent<RectTransform>().anchoredPosition -= new Vector2(-80, 0);
                pointer.enabled = false;
                break;
        }
    }
    public void NodeClick(bool isOn)
    {
        if (childern.Count != 0)
        {
            if (isOn)
            {
                //节点前方指针的旋转
                pointer.transform.localEulerAngles = new Vector3(0, 0, -90);
                //开启子节点
                for (int i = 0; i < childern.Count; i++)
                {
                    childern[i].gameObject.SetActive(true);
                    if (_nodeType == NodeType.Node)
                    {
                        childern[i].GetComponent<Toggle>().isOn = true;
                    }
                    //根节点开启行为
                    if (_nodeType == NodeType.RootNode)
                    {
                        OpenAllNodes();
                    }
                }
            }
            else
            {
                pointer.transform.localEulerAngles = new Vector3(0, 0, 0);
                for (int i = 0; i < childern.Count; i++)
                {
                    childern[i].gameObject.SetActive(false);
                    //记录开启状态
                    if (_nodeType == NodeType.Node)
                    { 
                        childern[i].GetComponent<Toggle>().isOn = false;
                    }
                    //根节点关闭
                    if (_nodeType == NodeType.RootNode)
                    {
                        RootCloseAllNodes();
                    }
                }
            }
        }
        else
        {
            print("没有子节点");
        }

    }
    /// <summary>
    /// 根节点关闭所有节点
    /// </summary>
    void RootCloseAllNodes()
    {
        for (int i= 1;i<transform.parent.childCount;i++)
        {
            transform.parent.GetChild(i).gameObject.SetActive(false);
        }
    }
    /// <summary>
    /// 根节点打开后还原关闭时候的分布
    /// </summary>
    void OpenAllNodes()
    {
        for (int i = 1; i < transform.parent.childCount; i++)
        {
            if (transform.parent.GetChild(i).GetComponent<NodeTreeInfo>()._nodeType == NodeType.LeafNode)
            {
                if (transform.parent.GetChild(i).GetComponent<NodeTreeInfo>().toggle.isOn)
                {
                    transform.parent.GetChild(i).gameObject.SetActive(true);
                }
            }
        }

    }


    //获取节点List<GameObject>
    public List<GameObject> GetChildernNodes(GameObject node)
    {
        List<GameObject> childernNode = new List<GameObject>();
        Transform nodeParent = node.transform.parent;
        if (node.GetComponent<NodeTreeInfo>()._nodeType == NodeType.RootNode)
        {
            for (int i = 0; i < nodeParent.childCount; i++)
            {
                if (nodeParent.GetChild(i).name == "F")
                {
                    childernNode.Add(nodeParent.GetChild(i).gameObject);
                }
            }
        }
        //判断Node下面的LeafNode
        if (node.name == "F")
        {
            //不是最后一个F节点 貌似不用了 hh
            int currentIndex = node.transform.GetSiblingIndex() + 1;
            //print(currentIndex + "     " + (nodeParent.childCount - currentIndex));
            for (int i = currentIndex; i < nodeParent.childCount; i++)
            {
                childernNode.Add(nodeParent.GetChild(i).gameObject);
                if (nodeParent.GetChild(i).GetComponent<NodeTreeInfo>()._nodeType == NodeType.Node)
                {
                    childernNode.RemoveAt(childernNode.Count - 1);
                    break;
                }
            }
        }
        return childernNode;
    }
}